﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Leiktuther.Models;

namespace Leiktuther.Controllers
{
    public class GameController : Controller
    {
        //
        // GET: /Game/
        private GameRepository _gameRepo = new GameRepository();
        private AppRepository _AppRepo = new AppRepository();

        public ActionResult Index()
        {
            return RedirectToAction("Index","Home");
        }
       


        /*
        public ActionResult Index(string reference)
        {

            return View();
        }
        */
        [Authorize]
        public ActionResult OlsenOlsen(int id)
        {
            AppDataContext db = new AppDataContext();

            var instance = (from game in db.GameInstances
                            where game.ID == id
                            select game).SingleOrDefault();

            if (instance != null)
            {
                return View(instance);
            }
            return View("_NotFound");
        }
        /*
        [HttpGet]
        [Authorize]
        public ActionResult OlsenOlsen(int id)
        {
            GameInstance gameInstance = _gameRepo.getInstanceById(id);
            return View();
        }
        
        
        [Authorize]
        public ActionResult OlsenOlsen(int? id)
        {
            return View();
        }
        */
        [Authorize]
        public ActionResult Connect4(int id)
        {

            AppDataContext db = new AppDataContext();

            var instance = (from game in db.GameInstances
                            where game.ID == id
                            select game).SingleOrDefault();

            if (instance != null)
            {
                return View(instance);
            }
            return View("_NotFound");
           
        }
        /*
        [Authorize]
        public ActionResult Connect4(int? id)
        {
            return View();
        }
        */
        public ActionResult GetGames(int id)
        {
            var instance = from inst in _AppRepo.Instances
                        where inst.GameID == id
                        select inst;
            return View(instance);
        }

        public ActionResult DeleteGame(int strengurinn)
        {
            using (AppDataContext db = new AppDataContext())
            {
                Game ga = db.Games.First(i => i.ID == strengurinn);
                db.Games.Remove(ga);
                db.SaveChanges();
                return View("Home", "Index");
            }
        }
    }
}
